const app = getApp()
import {
    queryVerificationCode,
    loginByVerificationCode,
    loginByWeChatCode
} from "../../service/user"
import {
    isPhoneValid
} from "../../utils/valid"

Page({
    data: {
        // 胶囊距离屏幕顶部的距离
        top: app.globalData.menuButtonTop,
        // 胶囊高度
        height: app.globalData.menuButtonHeight,
        // 手机号
        phone: "",
        // 验证码
        verificationCode: "",
        // 验证码倒计时
        countDown: 10,
        // 是否展示倒计时
        showCountDown: false,
        // 定时器实例
        timer: null
    },
    /**
     * 处理手机号输入
     */
    handlePhoneInput(e) {
        this.setData({
            phone: e.detail.value
        })
    },
    /**
     * 处理验证码输入
     */
    handleVerificationCodeInput(e) {
        this.setData({
            verificationCode: e.detail.value
        })
    },
    /**
     * 点击登录按钮
     */
    async onTapLogin() {
        wx.showLoading({
            title: '登陆中',
        })
        const res = await loginByVerificationCode(this.data.phone, this.data.verificationCode)
        wx.hideLoading()
        if (res.code === 0) {
            wx.showToast({
                title: '验证码错误，请重新输入',
                icon: "none"
            })
        } else {
            // 登录成功
            wx.showToast({
                title: '成功登录',
                icon: "none"
            })
            app.globalData.isLogin = true
            wx.setStorageSync('token', res.data);
            // 跳转到User页面
            wx.switchTab({
                url: '/pages/User/User',
            })
        }
    },
    /**
     * 点击清除按钮
     */
    onTapClearButton() {
        this.setData({
            phone: "",
            verificationCode: ""
        })
    },
    /**
     * 点击发送验证码
     */
    async onTapSendCode() {
        const phone = this.data.phone
        if (phone.length !== 11) return
        if (!isPhoneValid(phone)) {
            wx.showModal({
                content: "请输入正确的11位手机号，当前输入的号码位数不足或格式错误", // 长文本内容
                showCancel: false, // 隐藏“取消”按钮，仅保留“确定”
            });
            return
        }
        // 手机号格式正确，开始发送验证码
        wx.showLoading({
            title: '正在发送验证码'
        })
        await queryVerificationCode(this.data.phone)
        wx.hideLoading()
        // 倒计时操作
        // 清除之前的定时器（防止多次点击创建多个定时器）
        if (this.data.timer) {
            clearInterval(this.data.timer);
        }
        this.setData({
            showCountDown: true,
            timer: setInterval(() => {
                const current = this.data.countDown;
                if (current <= 0) {
                    // 倒计时结束
                    clearInterval(this.data.timer);
                    this.setData({
                        countDown: 10,
                        timer: null,
                        showCountDown: false
                    });
                } else {
                    this.setData({
                        countDown: current - 1
                    })
                }
            }, 1000)
        })
    },
    /**
     * 点击左上角返回键
     */
    onTapArrowLeft() {
        wx.switchTab({
            url: '/pages/User/User',
        })
    },
    /**
     * 点击微信登录
     */
    onTapWechatLogin() {
        wx.login({
            async success(res) {
                const code = res.code
                const {
                    data: token
                } = await loginByWeChatCode(code)
                wx.showToast({
                    title: '成功登录',
                    icon: "none"
                })
                app.globalData.isLogin = true
                wx.setStorageSync('token', token);
                wx.switchTab({
                    url: '/pages/User/User',
                })
            }
        });
    }

})